﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormLoaiKho : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        public FormLoaiKho()
        {
            InitializeComponent();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormLoaiKhoThem();
            f.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var f = new FormLoaiKhoCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListLoaiKho();
            KiemTra();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormLoaiKho_Load(object sender, EventArgs e)
        {
            GetListLoaiKho();
            KiemTra();
        }

        //private void dgvLoaiKho_CellClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    int rowindex = e.RowIndex;
        //    DataGridViewRow row = dgvLoaiKho.Rows[rowindex];
        //    idtam = Convert.ToInt32(row.Cells[0].Value);
        //    KiemTra();
        //}

        private void dgvLoaiKho_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiKho.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
        }
        //private void dgvLoaiKho_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    int rowindex = e.RowIndex;
        //    DataGridViewRow row = dgvLoaiKho.Rows[rowindex];
        //    idtam = Convert.ToInt32(row.Cells[0].Value);
        //    KiemTra();
        //    var f = new FormLoaiKhoCapNhap();
        //    f.id = idtam;
        //    f.ShowDialog();
        //}
        private void dgvLoaiKho_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            DataGridViewRow row = dgvLoaiKho.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormLoaiKhoCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }
        void GetListLoaiKho()
        {
            dgvLoaiKho.DataSource = db.LoaiKhoHangs.ToList();
            idtam = 0;
        }

        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        void Delete()
        {
            try
            {
                var t = db.LoaiKhoHangs.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa nhóm: " + t.TenLoaiKhoHang + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    db.LoaiKhoHangs.Remove(t);
                    db.SaveChanges();
                    GetListLoaiKho();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }
    }
}
